var e = require("@babel/runtime/helpers/interopRequireDefault.js"), n = e(require("@babel/runtime/regenerator.js")), o = e(require("@babel/runtime/helpers/asyncToGenerator.js")), r = require("qiniu.js"), t = require("mime-types.js"), a = require("fs.js"), i = require("path.js"), u = require("chalk.js"), c = process.argv, l = new r.auth.digest.Mac("fxWBx5v67yGBBJjAbSqpypKP0WTOiP03iwKvVMdW", "wvHdVZzKbVzM7LKBCv6A1gOW2taDhNn6a6JumBfy"), s = new r.rs.PutPolicy({
    scope: "image"
}).uploadToken(l), f = new r.conf.Config();

f.zone = r.zone.Zone_z0;

var p, d = new r.form_up.FormUploader(f), g = new r.rs.BucketManager(l, f), m = (new r.cdn.CdnManager(l), 
function(e, n, o) {
    var a = t.lookup(e), i = new r.form_up.PutExtra();
    i.MimeType = a, d.putFile(s, n, e, i, function(e, n, r) {
        if (e) throw e;
        o && o(n, r);
    });
}), y = function(e) {
    g.delete("image", e, function(e, n, o) {
        e ? console.log(u.red(e)) : (console.log(o.statusCode), console.log(n));
    });
}, h = [], w = 0;

function v(e) {
    return new Promise(function(n, o) {
        m(e.path, e.key, function(r, t) {
            200 == t.statusCode ? (console.log(u.cyan("cdn文件地址 : https://img.9liuda.com/" + e.key)), 
            n(!0)) : (console.log(u.red(JSON.stringify(r))), console.log(u.red("失败文件地址 : https://img.9liuda.com/" + e.key)), 
            o(!1));
        });
    });
}

if (c.length > 2) {
    var q = c[2], b = c[3];
    "delete" == q && (p = "image/baiduapp/weather/images/" + b, console.log(p), y(p));
} else !function e(n) {
    var o = "image/douyin/testSmall/";
    try {
        a.readdirSync(n).forEach(function(r) {
            var t = i.join(n, r).replace("\\", "/");
            a.statSync(t).isFile() ? h.push({
                path: i.join(__dirname, t),
                key: o + t,
                destKey: o + t + "?time=" + Date.now(),
                name: r
            }) : e(t);
        });
    } catch (e) {
        console.log(e);
    }
}(i.join("images")), u.yellow("正在上传静态文件.\n"), h.forEach(function() {
    var e = (0, o.default)(n.default.mark(function e(o, r) {
        return n.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                return e.next = 2, v(o);

              case 2:
                e.sent ? ++w == h.length && console.log(u.cyan("共上传了".concat(w, "个文件"))) : console.log(u.red("文件上传失败  " + o.name));

              case 4:
              case "end":
                return e.stop();
            }
        }, e);
    }));
    return function(n, o) {
        return e.apply(this, arguments);
    };
}());